package com.pulaien.shop.controller;

import com.pulaien.common.core.controller.BaseController;
import com.pulaien.common.core.domain.AjaxResult;
import com.pulaien.common.core.page.TableDataInfo;
import com.pulaien.common.exception.ApiException;
import com.pulaien.common.utils.ConstantsUtil;
import com.pulaien.common.utils.MessageUtils;
import com.pulaien.common.utils.RandomValidateCodeUtil;
import com.pulaien.common.utils.SecurityUtils;
import com.pulaien.common.utils.validator.ValidationGroups;
import com.pulaien.system.domain.Feedback;
import com.pulaien.system.domain.dto.*;
import com.pulaien.system.service.IFeedbackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@Api(value = "商城端售后反馈")
@RequestMapping("/shop/feedBack")
public class ShopFeedbackController extends BaseController {

    @Resource
    IFeedbackService feedbackService;

    @ApiOperation(value = "列表")
    @GetMapping("/list")
    public TableDataInfo selectAll(ExtFeedbackDto Feedback, @RequestHeader(ConstantsUtil.HEAD_LANGUAGE)String headLanguage) {
        startPage();
        List<ExtFeedbackVo> mvo =feedbackService.list(Feedback);
        return getDataTable(mvo);
    }

    @ApiOperation("查询统计数量")
    @RequestMapping(value = "/getStatisticalNum",method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getStatisticalNum(Feedback feedback){
        feedback.setUserId(SecurityUtils.getUserId());
        ExtFeedBackNum result = feedbackService.getStatisticalNum(feedback);
        return AjaxResult.success(result);
    }

    @ApiOperation(value = "查询详情")
    @GetMapping("/info/{id}")
    public AjaxResult selectInfo(@PathVariable Long id,@RequestHeader(ConstantsUtil.HEAD_LANGUAGE)String headLanguage) {
        return feedbackService.info(id,headLanguage);
    }

    @ApiOperation(value = "新建反馈")
    @PostMapping("/add")
    public AjaxResult submit(@RequestBody @Validated(ValidationGroups.add.class) Feedback Feedback) {
        return feedbackService.add(Feedback);
    }

    @ApiOperation(value = "编辑反馈")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody  Feedback Feedback) {
        return feedbackService.update(Feedback);

    }

    @ApiOperation(value = "取消")
    @PostMapping("/cancel/{id}")
    public AjaxResult delList(@PathVariable Long id) {
        return feedbackService.cancel(id);
    }

    @ApiOperation(value = "获取图片验证码")
    @GetMapping(value = "/verifyCode", produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public void getVerify(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("image/jpeg");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
        try {
            randomValidateCode.getRandomCode(request, response, ConstantsUtil.FREEBACK_KEY);//输出验证码图片方法
        } catch (Exception e) {
            throw new ApiException("验证码生成失败");
        }
    }

    @ApiOperation(value = "验证验证码")
    @GetMapping(value = "checkValidCode")
    public AjaxResult checkValidCode(HttpSession session, @RequestParam String code) {
        String validCode = session.getAttribute(ConstantsUtil.FREEBACK_KEY) == null ? "" : session.getAttribute(ConstantsUtil.FREEBACK_KEY).toString();
        if (StringUtils.isNotBlank(code) && StringUtils.isNotBlank(validCode) && code.toLowerCase().equals(validCode.toLowerCase())) {
            return AjaxResult.success("验证成功");
        }
        return AjaxResult.success(MessageUtils.getMessage(10304));
    }
}
